/**
 * @(#)SerializadorXstream.java 1/04/2010
 *
 * Copyright (c) 2005-2010 PsiqueWare S.A. de C.V. All Rights Reserved.
 * This software is the proprietary information PsiqueWare S.A. de C.V.
 * Use is subject to license terms.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 *
 * <br/>Usuario : AngelElectrico
 * <br/>Creaci&oacute;n: 1/04/2010 - 10:54:32 AM
 * <br/>
 * <br/>
 * <br/><t5>Cambios :</t5>
 * <br/><b>Fecha | Autor  |  Cambios</b>
 * <ul>
 * <li>1/04/2010 | <a href = "mailto:AngelElectrico@PsiqueWare.com">&Aacute;ngel El&eacute;ctrico</a> | Creaci&oacute;n del Objeto.</li>
 *
 * </ul>
 */

package com.psiqueware.alheli.serializacion.serializadores;

import com.psiqueware.alheli.serializacion.Serializador;
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.DomDriver;

import java.io.Serializable;

/**
 * {@link Serializador} basado en {@link XStream}.
 * <br/>Paquete Original: com.psiqueware.alheli.serializacion.xstream
 * <br/>Nombre  Original: SerializadorXstream
 * <br/>Oprima para ver : <a href="SerializadorXstream.java.html"><i>C&oacute;digo Fuente</i> </a>
 *
 * @author <a href="mailto:AngelElectrico@PsiqueWare.com">AngelElectrico</a>
 * @version Revision: 1.1 1/04/2010 10:54:32 AM
 */
public class SerializadorXStream implements Serializador
{

    private XStream m_xstream;


    /**
     * Constructor de SerializadorXstream.
     */
    public SerializadorXStream()
    {

    }


    /**
     * Convierte un {@link Serializable} a su representaci&oacute;n en una cadena.
     *
     * @param serializable a convertir.
     *
     * @return cadena con el objeto serializado.
     */
    public String serializar(final Serializable serializable)
    {
        synchronized (this)
        {
            if (null == m_xstream)
            {
                m_xstream = getInstance();
            }
        }

        return m_xstream.toXML(serializable);
    }

    /**
     * Convierte una cadena que representa un {@link Serializable} a su objeto representadol.
     *
     * @param cadena a convertir.
     *
     * @return el {@link Serializable} convertido.
     */
    public Serializable objetivizar(final String cadena)
    {
        synchronized (this)
        {
            if (null == m_xstream)
            {
                m_xstream = getInstance();
            }
        }
        return (Serializable) m_xstream.fromXML(cadena);
    }

    /**
     * Devuelve una instance de {@link XStream} inicializada correctamente.
     *
     * @return instancia de {@link XStream}.
     */
    protected XStream getInstance()
    {
        return new XStream(new DomDriver());
    }


}

